"""add wireguard fields

Revision ID: 74e856afa9cc
Revises: 
Create Date: 2025-10-10 19:18:12.521902

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = '74e856afa9cc'
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    """Upgrade schema - 只添加WireGuard字段，不创建表"""

    # 为nodes表添加WireGuard字段（如果不存在）
    op.add_column('nodes', sa.Column('wg_public_key', sa.String(length=255), nullable=True))
    op.add_column('nodes', sa.Column('wg_private_key', sa.String(length=255), nullable=True))
    op.add_column('nodes', sa.Column('wg_endpoint', sa.String(length=100), nullable=True))
    op.add_column('nodes', sa.Column('wg_port', sa.Integer(), server_default='51820', nullable=True))
    op.add_column('nodes', sa.Column('subnet', sa.String(length=20), server_default='10.0.0.0/24', nullable=True))
    op.add_column('nodes', sa.Column('dns_servers', sa.String(length=255), server_default='8.8.8.8,1.1.1.1', nullable=True))

    # 为user_subscriptions表添加WireGuard字段（如果不存在）
    op.add_column('user_subscriptions', sa.Column('wg_public_key', sa.String(length=255), nullable=True))
    op.add_column('user_subscriptions', sa.Column('wg_private_key', sa.String(length=255), nullable=True))
    op.add_column('user_subscriptions', sa.Column('client_ip', sa.String(length=20), nullable=True))


def downgrade() -> None:
    """Downgrade schema - 只移除WireGuard字段"""
    op.drop_column('user_subscriptions', 'client_ip')
    op.drop_column('user_subscriptions', 'wg_private_key')
    op.drop_column('user_subscriptions', 'wg_public_key')

    op.drop_column('nodes', 'dns_servers')
    op.drop_column('nodes', 'subnet')
    op.drop_column('nodes', 'wg_port')
    op.drop_column('nodes', 'wg_endpoint')
    op.drop_column('nodes', 'wg_private_key')
    op.drop_column('nodes', 'wg_public_key')